package com.qy.action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.qy.model.PageModel;
import com.qy.model.User;
import com.qy.model.Works;
import com.qy.service.UserService;

@Controller("userAction")
@ParentPackage("json-default")
public class UserAction {
	private String sel;
	private File file;
	private String fileFileName;
	private String fileContentType;
	@Autowired
	private PageModel pagemodel;
	
	public PageModel getPagemodel() {
		return pagemodel;
	}

	public void setPagemodel(PageModel pagemodel) {
		this.pagemodel = pagemodel;
	}
	private List<Works> workslist = new ArrayList<>();
	
	public List<Works> getWorkslist() {
		return workslist;
	}

	public void setWorkslist(List<Works> workslist) {
		this.workslist = workslist;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	private List<User> users = new ArrayList<>();
	@Autowired
	private UserService userService;
	private Map<String, Object> result = new HashMap<>();
	@Autowired
	private User user;

	private Works works;

	public Works getWorks() {
		return works;
	}

	public void setWorks(Works works) {
		this.works = works;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public String getSel() {
		return sel;
	}

	public void setSel(String sel) {
		this.sel = sel;
	}

	@Action(value = "register", results = { @Result(type = "json", params = { "root", "result" }) })
	public String register() throws Exception {
		// TODO Auto-generated method stub
		File file1 = new File("/Users/qingyun/Desktop/film/film/WebContent/images");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		  String datePath = sdf.format(new Date());
		  File filepath = new File(file1+"/"+datePath);
		  if(!filepath.exists()){
			  filepath.mkdirs();
		  }
		  long time = System.currentTimeMillis();
		  String filename = file1+"/"+datePath+"/"+time+fileFileName;
		  File file2 = new File(filename);
		  String filename2 = "images/"+datePath+"/"+time+fileFileName;
		  user.setUrl(filename2);
		  userService.savefile(file, file2);
		  user.setIdentity("用户");
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		user.setStartTime(timestamp);
		try {
			userService.save(user);
			result.put("isSuccess", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("isSuccess", false);
		}
		return "success";
	}

	@Action(value = "login", results = { @Result(type = "json", params = { "root", "result" }) })
	public String login() {
		// TODO Auto-generated method stub
		try {
			String hql = "from com.qy.model.User user where user.username=? and user.password =? and user.state=? ";
			Object object[] = new Object[3];
			object[0] = user.getUsername();
			object[1] = user.getPassword();
			object[2] = 0;
			users = userService.select(hql, object);
			if (users.size() > 0) {
				result.put("isSuccess", true);
				Map<String,Object> session = ActionContext.getContext().getSession();
				session.put("user", users.get(0));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("isSuccess", false);
		}
		return "success";
	}

	@Action(value = "selectindex", results = { @Result(type = "json", params = { "root", "users" }) })
	public String selectindex() {
		// TODO Auto-generated method stub
		try {
			String hql = "from com.qy.model.User user where user.state=?";
			Object object[] = new Object[1];
			object[0]=0;
			users = userService.select(hql, object);
			for (User user : users) {
				user.setAdress(null);
				user.setAppraises(null);
				user.setCameraPeople(null);
				user.setWithdraws(null);
				user.setMessages(null);
				user.setOrders(null);
				user.setWorks(null);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	@Action(value = "selectindex_1", results = { @Result(type = "json", params = { "root", "users" }) })
	public String selectindex1() {
		// TODO Auto-generated method stub
		String hql = "from com.qy.model.User user where user.state=?";
		String hql1 = "from com.qy.model.User user where user.identity=?";
		String hql2 = "from com.qy.model.User user where user.identity=?";
		try {
			if (sel.equals("18")) {
				Object object[] = new Object[1];
				object[0]=0;
				users = userService.select(hql, object);
			} else if(sel.equals("19")){
				Object object[] = new Object[1];
				object[0]="用户";
				users = userService.select(hql1, object);
			}else {
				Object object[] = new Object[1];
				object[0]="摄影师";
				users = userService.select(hql2, object);
			}
			for (User user : users) {
				user.setAdress(null);
				user.setAppraises(null);
				user.setCameraPeople(null);
				user.setWithdraws(null);
				user.setMessages(null);
				user.setOrders(null);
				user.setWorks(null);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}

	@Action(value = "closeuser", results = { @Result(type = "json", params = { "root", "result" }) })
	public String deleteuser() {
		// TODO Auto-generated method stub
		try {
			String hql ="update com.qy.model.User user set user.state=? where user.id=?";
			List<Object> list = new ArrayList<>();
			list.add(1);
			list.add(user.getId());
			userService.execute(hql, list);
			result.put("isSuccess", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("isSuccess", false);
		}
		return "success";
	}
	@Action(value = "saveWorks", results = { @Result(type = "json", params = { "root", "result" }) })
	public String saveWorks() {
		// TODO Auto-generated method stub
		try {
			Map session = ActionContext.getContext().getSession();
			User user = (User) session.get("user"); 
			if (user==null) {
				result.put("isSuccess", "请您先登录");
			}else{
				works.setUser(user);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String datePath = sdf.format(new Date());
				File filepath = new File("/Users/qingyun/Desktop/film/film/WebContent/images"+"/works/"+datePath);
				if(!filepath.exists()){
					  filepath.mkdirs();
				  }
				File file2 = new File(filepath+"/"+System.currentTimeMillis()+fileFileName);
				String filename2 = "images/works/"+datePath+"/"+System.currentTimeMillis()+fileFileName;
				works.setPrhotourl(filename2);
				userService.savefile(file, file2);
				Timestamp timestamp = new Timestamp(System.currentTimeMillis());
				works.setPhototime(timestamp);
				userService.save(works);
				result.put("isSuccess", true);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch bloc
			e.printStackTrace();
			result.put("isSuccess", false);
		}
		return "success";
	}
	@Action(value = "upcamerapeople", results = { @Result(type = "json", params = { "root", "result" }) })
	public String upcamerapeople() {
		// TODO Auto-generated method stub
		try {
			Map session = ActionContext.getContext().getSession();
			User user1 = (User) session.get("user"); 
			if (user1==null) {
				result.put("isSuccess", "请您先登录");
			}else{
				String hql ="update com.qy.model.User user set user.identity=? where user.id=?";
				List<Object> list = new ArrayList<>();
				list.add("摄影师");
				list.add(user.getId());
				userService.execute(hql, list);
				result.put("isSuccess", true);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch bloc
			e.printStackTrace();
			result.put("isSuccess", false);
		}
		return "success";
	}
	@Action(value = "selectproducts", results = { @Result(type = "json", params = { "root", "result" }) })
	public String selectproducts() {
		// TODO Auto-generated method stub
		try {
			pagemodel.setPageSize(10);
			String hql = "from com.qy.model.Works works where works.state=?";
			Object object[] = new Object[1];
			object[0]=0;
			List<Works> list = new ArrayList<>();
			list = userService.select(hql, object);
			if (list.size()%pagemodel.getPageSize()==0) {
				pagemodel.setTotalPage(list.size()/pagemodel.getPageSize());
			}else{
				pagemodel.setTotalPage(list.size()/pagemodel.getPageSize()+1);
			}
			workslist = userService.selectAllByPage(hql, object, pagemodel);
			result.put("total", pagemodel);
			for (Works work : workslist) {
				work.getUser().setAdress(null);
				work.getUser().setAppraises(null);
				work.getUser().setCameraPeople(null);
				work.getUser().setWithdraws(null);
				work.getUser().setMessages(null);
				work.getUser().setOrders(null);
				work.getUser().setWorks(null);
			}
			result.put("result", workslist);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	@Action(value = "selectproducts_1", results = { @Result(type = "json", params = { "root", "result" }) })
	public String selectproducts_1() {
		// TODO Auto-generated method stub
		String hql = "from com.qy.model.Works works where works.state=?";
		String hql1 = "from com.qy.model.Works works where works.state=?";
		List<Works> list = new ArrayList<>();
		try {
			Object object[] = new Object[1];
			if (sel.equals("18")||sel.equals("19")) {
				object[0]=0;
				list = userService.select(hql, object);
				workslist = userService.selectAllByPage(hql,object, pagemodel);
			}else{
				object[0]=1;
				list = userService.select(hql1, object);
				workslist = userService.selectAllByPage(hql1,object, pagemodel);
			}
			pagemodel.setPageSize(10);
			if (list.size()%pagemodel.getPageSize()==0) {
				pagemodel.setTotalPage(list.size()/pagemodel.getPageSize());
			}else{
				pagemodel.setTotalPage(list.size()/pagemodel.getPageSize()+1);
			}
			result.put("total", pagemodel);
			for (Works work : workslist) {
				work.getUser().setAdress(null);
				work.getUser().setAppraises(null);
				work.getUser().setCameraPeople(null);
				work.getUser().setWithdraws(null);
				work.getUser().setMessages(null);
				work.getUser().setOrders(null);
				work.getUser().setWorks(null);
			}
			result.put("result", workslist);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	@Action(value = "updateworks", results = { @Result(type = "json", params = { "root", "works" }) })
	public String updateworks() {
		// TODO Auto-generated method stub
		String hql = "from tb_works works where works.id= ?";
		Object object[]= new Object[1];
		object[0]=works.getId();
		try {
			workslist = userService.select(hql, object);
			works = workslist.get(0);
			works.getUser().setAdress(null);
			works.getUser().setAppraises(null);
			works.getUser().setCameraPeople(null);
			works.getUser().setWithdraws(null);
			works.getUser().setMessages(null);
			works.getUser().setOrders(null);
			works.getUser().setWorks(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	@Action(value = "worksstate", results = { @Result(type = "json", params = { "root", "result" }) })
	public String worksstate() {
		// TODO Auto-generated method stub
		String hql = "update com.qy.model.Works works set works.state=? where works.id=?";
		Object object[]= new Object[2];
		List<Object> list = new ArrayList<>();
		list.add(1);
		list.add(works.getId());
		try {
			userService.execute(hql, list);
			result.put("isSuccess", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("isSuccess", false);
		}
		return "success";
	}
	@Action(value = "updatesaveworks", results = { @Result(type = "json", params = { "root", "result" }) })
	public String updatesaveworks() {
		// TODO Auto-generated method stub
		try {
			String hql="update com.qy.model.Works works set works.cameramodel=?,works.info=?,works.location=?,"
					+ "works.name=?,works.phototime=?,works.prhotourl=?,works.price=?,works.state=?,works.styletype=?,"
					+ "works.autor=? where works.id=?";
			List<Object> list = new ArrayList<>();
			list.add(works.getCameramodel());
			list.add(works.getInfo());
			list.add(works.getLocation());
			list.add(works.getName());
			list.add(works.getPhototime());
			list.add(works.getPrhotourl());
			list.add(works.getPrice());
			list.add(works.getState());
			list.add(works.getStyletype());
			list.add(works.getAutor());
			list.add(works.getId());
			userService.execute(hql, list);
			result.put("isSuccess", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("isSuccess", false);
		}
		return "success";
	}
}
